//
//  SettingVC.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/14.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class SettingVC: UITableViewController {

    var dataArr = NSMutableArray()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
        
        
        dataArr = ["个人信息","退出"]

        self.navigationItem.title = "设置"
        
        //设置表格样式
        tableView = UITableView.init(frame: self.view.bounds, style: .grouped)
    }


   }

// MARK: - Table view data source
extension SettingVC {

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return dataArr.count
        
    }
}

// MARK: - Table view delegate
extension SettingVC {
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cellID = "cell";
        
        let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: cellID)
        
        cell.textLabel?.text = String(dataArr[indexPath.row] as! String)
        
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print("单机表格")
        print(indexPath.row)
        
        switch indexPath.row {
        //跳转到个人信息界面
        case 0:
            let perVC = PersonInfoVC()
            self.navigationController?.pushViewController(perVC, animated: true)
            break
            
        case 1:
            //退出登陆
            logoutSEL()
            
            break
        default:
            break
        }
    }

}

// MARK:- 退出函数
extension SettingVC {
    func logoutSEL() -> Void {


        
        if localSave.object(forKey: userToken) as? String != nil {
            let uToken = localSave.object(forKey: userToken) as! String
            
            let param = ["token" : uToken] as [String : String]
            print("\((#file as NSString).lastPathComponent):(\(#line))")
            
            print(param)
            
            
            postWithPath(path: logoutURL, paras: param, success: { (response) in
                //判读返回值是否为空
                guard let dic = response as? NSDictionary else {
                    return
                }
                
                print(dic)
                
                //提取提示语
                let alertmsg = dic["resultmsg"] as! String
                
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                print(dic)
                print(dic["resultmsg"] as! String)
                print(response)
                
                //移除通知函数
                NotificationCenter.default.removeObserver(self)
                
                if alertmsg  == "退出成功" {
                    MBManager.showBriefAlert("退出成功")
                    
                    
                    
                    //根据tag取出账号文本


                    //发送通知
                    //通知名称常量
                    let logoutNotify = NSNotification.Name(rawValue:logoutNotifi)
                    NotificationCenter.default.post(name:logoutNotify, object: self)
                    
                    //删除本地token
                    localSave.removeObject(forKey: userToken)
                    localSave.synchronize()
                    
                    self.navigationController?.popToRootViewController(animated: true)
                    

                    
                } else if alertmsg == "该账号已在异地登录，请重新登录" {
                    MBManager.showBriefAlert(loginError)
                }
                
            }, failure: { (error) in
                print(error.localizedDescription)
            })
            
        } else {
            MBManager.showBriefAlert("已退出")
//            myHeaderLabel.text = "单机登陆"
//            myHeaderHeadImg.image = UIImage.init(named: "qq")
            
            print("token不存在")
        }
        
    }
}


